﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AvalonDock;
using System.Windows.Threading;
using RiskMan.DataClass.Models.Gilevskiy;
using RiskMan.Config;
using RiskMan.Views.Dialogs;
using System.Collections.ObjectModel;

namespace RiskMan.Views.Gilevskiy
{
    /// <summary>
    /// Interaction logic for TimeFramesWinodw.xaml
    /// </summary>
    public partial class TimeFramesWinodw : DocumentContent
    {
        private readonly MainWindow mainWindow;
        //ObservableCollection<TimeFrameItem> tmpCollForTimeFrame;
        public TimeFramesWinodw()
        {
            mainWindow = MainWindow.StaticLevel;
            InitializeComponent();

            DtgdTimeFrameItems.ItemsSource = mainWindow._dataProvider.TimeFrameItems;
            //tmpCollForTimeFrame = mainWindow._dataProvider.TimeFrameItems;
        }

        private void BtnCopy_Click(object sender, RoutedEventArgs e)
        {
            if (DtgdTimeFrameItems.SelectedItems.Count == 1)
            {
                int index = DtgdTimeFrameItems.SelectedIndex + 1;
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите копировать строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow._dataProvider.TimeFrameItems)
                        {
                            if (item.GuidItem == ((TimeFrameItem)(DtgdTimeFrameItems.SelectedItem)).GuidItem)
                            {
                                var tmp = (TimeFrameItem)Cfg.DeepClone(item);
                                tmp.GuidItem = Guid.NewGuid();
                                mainWindow._dataProvider.TimeFrameItems.Add(tmp);
                            }
                        }

                    }
                }
                catch
                { }
            }

        }

        private void Button_AddRow_Click(object sender, RoutedEventArgs e)
        {
            TimeFrameItem item = new TimeFrameItem();
            AddNewTimeFrame window = new AddNewTimeFrame(item, mainWindow._dataProvider);
            window.ShowDialog();
            if (window.DialogResult == true)
            {
                mainWindow._dataProvider.TimeFrameItems.Add(item);
                TransformTimeFrames.NumberRowAllTrades = 0;
                Cfg.SetLog("Добавлены новые условия", mainWindow.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (DtgdTimeFrameItems.SelectedItems.Count == 0)
                new DialogMessage("Не выбрано ни одного элемента для удаления", "Внимание!");

            if (DtgdTimeFrameItems.SelectedItems.Count == 1)
            {
                int index = DtgdTimeFrameItems.SelectedIndex + 1;
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите удалить строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow._dataProvider.TimeFrameItems)
                        {
                            if (item.GuidItem == ((TimeFrameItem)(DtgdTimeFrameItems.SelectedItem)).GuidItem)
                            {
                                mainWindow._dataProvider.TimeFrameItems.Remove(item);
                                break;
                            }
                        }

                    }
                }
                catch
                { }
            }

        }


        private void DtgdTimeFrameItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (DtgdTimeFrameItems.SelectedIndex >= 0)
            {
                TimeFrameItem ldItem = ((DataGrid)sender).SelectedItem as TimeFrameItem;
                AddNewTimeFrame addNewRobot = new AddNewTimeFrame(ldItem, mainWindow._dataProvider);
                addNewRobot.ShowDialog();
            }
        }

        private void DtgdTimeFrameItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //for (int i = 0; i < mainWindow._dataProvider.TimeFrameItems.Count; i++)
            //{
            //    if (((DataGrid) sender).SelectedIndex == i)
            //    {
            //        DtgdCandles.ItemsSource =
            //             mainWindow._dataProvider.TimeFrameItems[i].CandleItems.OrderByDescending(a => a.TradeDateTime);


            //    }
            //}
        }

        //
    }
}
